package com.ssyc.student.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StAchievementRunningAdapter;
import com.ssyc.student.bean.AchievementRunningInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


/**
 * 学生端--成就--进行中成就
 */

public class StudentAchievementRunningFragment extends LazyBaseFragment implements SrfManager.OnPullRefreshListener {

    //加载框
    private RelativeLayout rlLoading;
    //下拉刷新控件
    private SmartRefreshLayout srf;
    //列表控件
    private RecyclerView rv;
    //列表的数据集合
    private List<AchievementRunningInfo.Data1Bean> oldDatas;

    //空视图
    private View emptyView;

    //适配器
    private StAchievementRunningAdapter adapter;

    //单例模式
    private static volatile StudentAchievementRunningFragment studentAchievementRunningFragment;

    public static StudentAchievementRunningFragment newInstance() {
        if (studentAchievementRunningFragment == null) {
            synchronized (StudentAchievementRunningFragment.class) {
                if (studentAchievementRunningFragment == null) {
                    studentAchievementRunningFragment = new StudentAchievementRunningFragment();
                }
            }
        }
        return studentAchievementRunningFragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_achievement_running;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

        showContent();

        initView(view);

        //设置下拉控件
        initSrl();

        //设置rv
        initRv();

        //请求服务器获取数据
        http(BqaManager.NORML);
    }


    /**
     * 设置下拉刷新控件
     */
    private void initSrl() {
        SrfManager.setSmartByPullListener(srf, this);
        srf.setPrimaryColors(getContext().getResources().getColor(R.color.transparent),getContext().getResources().getColor(R.color.white));
    }


    /**
     * 初始化View
     */
    private void initView(View view) {
        srf = view.findViewById(R.id.srf);
        rv = view.findViewById(R.id.rv);
        rlLoading = view.findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
    }


    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(getContext(), R.layout.base_rv_common_empty_view, null);
        adapter = new StAchievementRunningAdapter(getContext(), R.layout.student_rv_achievement_running, oldDatas);
    }


    /**
     * 请求服务器获取数据
     *
     * @param method
     */
    private void http(int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "7");
        map.put("acc", AccountUtils.getAccount(getActivity()));
        map.put("role", AccountUtils.getRole(getActivity()));
        map.put("platform", "8");
        map.put("apptoken",AccountUtils.getToken(getActivity()));
        Log.i("test","type=7&acc="+AccountUtils.getAccount(getActivity())+"&role="+AccountUtils.getRole(getActivity())
        +"&platform=8&apptoken="+AccountUtils.getToken(getActivity()));

        HttpUtils.post(HttpAdress.ACHIEVEMENT, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    AchievementRunningInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, AchievementRunningInfo.class);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("test", "数据解析异常");
                    }

                    if (null != info) {
                        List<AchievementRunningInfo.Data1Bean> newDatas = info.getData1();
                        if (method == BqaManager.NORML) {
                            oldDatas.addAll(newDatas);
                            BqaManager.setRv(emptyView, getContext(), adapter, rv);
                        } else {
                            BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                            adapter.setEnableLoadMore(false);
                        }
                    }
                }
            }
        });
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

}
